const BusinessError = require('./businessError');
const ErrorCode = require('./ErrorCode');

/**
 * the business error proxy
 * @author thomas.kang@hrx.ai
 */
class BusinessExceptionProxy {
  /**
   * constructor
   */
  constructor() {
    this.ErrorCode = ErrorCode;
  }

  /**
   * @throws User is not existed exception
   * @param {String} username
   */
  userNotExisted(username) {
    throw new BusinessError(ErrorCode.USER_NOT_FOUND, `The user [${username}] is not existed`);
  }

  /**
   * @param {Array} errorItems
   */
  parameterValueError(errorItems) {
    throw new BusinessError(ErrorCode.PARAMETER_ERROR, 'Parameter value error', errorItems);
  }

  /**
   * @throws invalid order status
   * @param {OrderStatus} status
   */
  invalidOrderStatus(status) {
    throw new BusinessError(
      ErrorCode.INVALIED_ORDER_STATUS,
      `Not allow this operation, since the order status was ${status}`
    );
  }

  /**
   * @throws not allow cancel order with init trades
   * @param {Integer} size: init trade size
   */
  notAllowCancelOrder(size) {
    throw new BusinessError(
      ErrorCode.HAS_INIT_TRADES,
      `Not allow cancel order, since have init trade${size > 1 ? 's' : ''}`
    );
  }

  /**
   * @throws unknown resource
   */
  unknownResource() {
    throw new BusinessError(ErrorCode.UNKNOWN_RESOURCE, 'unknown resource');
  }

  /**
   * @throws unVerifiedPhoneNumber
   */
  unVerifiedPhoneNumber() {
    throw new BusinessError(ErrorCode.UN_VERIFIED_PHONE_NUMBER, 'Please verify your phone number');
  }

  /**
   * @param {String} type
   */
  unSupportType(type) {
    throw new BusinessError(ErrorCode.UN_SUPPORT, 'Unsupported type [' + type + ']');
  }

  /**
   * @throws unknown sign in username
   */
  unknownAuthorizer() {
    throw new BusinessError(ErrorCode.UNKNOWN_AUTHORIZER, 'unknow authorizer');
  }

  /**
   * @param {String} field
   */
  requiredAndNotNull(field) {
    throw new BusinessError(
      ErrorCode.REQUIRED_AND_NOT_NULL,
      `[${field}] is required or can not be null`
    );
  }

  /**
   * @throws blocking user
   */
  blockingUser() {
    throw new BusinessError(ErrorCode.BLOCKING_USER, 'decline request, since blocking user');
  }

  /**
   * @param {String} coinType
   */
  balanceNotEnough(coinType) {
    throw new BusinessError(ErrorCode.BALANCE_NOT_ENOUGH, `${coinType} balance not enough`);
  }

  /**
   * @throws common exception
   * @param {Integer} code
   * @param {String} msg
   */
  commonBusinessException(code, msg) {
    throw new BusinessError(code, msg);
  }
}

module.exports = new BusinessExceptionProxy();
